<?php

class PageAvantAccueil extends Module
{
	function __construct()
	{
		$this->name = 'pageavantaccueil';
		$this->tab = 'Advertisement';
		$this->version = 1.1;
		parent::__construct();
		$this->displayName = $this->l('Prior Home Page');
		$this->description = $this->l('Add a prior home page');
	}

	function install()
	{
		if (
				!parent::install()
			||	!$this->registerHook('home')
			||	!Configuration::updateValue('pageavantaccueil_activate', 0)
			||	!Configuration::updateValue('pageavantaccueil_time_cookie', 900)
			||	!Configuration::updateValue('pageavantaccueil_prior_home_page', 'index.php')
			)
			return false;
		return true;
	}

	function hookHome($params)
	{
		if(Configuration::get('pageavantaccueil_activate')) {
			if(!isset($_COOKIE['pageavantaccueil_page_load'])) {
				setcookie('pageavantaccueil_page_load', '1', (time() + intval(Configuration::get('pageavantaccueil_time_cookie'))));
				Tools::redirect('./'.Configuration::get('pageavantaccueil_prior_home_page'));
			}
		}
		else {
			if(isset($_COOKIE['pageavantaccueil_page_load'])) unset($_COOKIE['pageavantaccueil_page_load-']);
		}
	}
	
	function ScanDirectory($Directory, $Tab)
	{
		$output = '';
		$MyDirectory = opendir($Directory) or die('Erreur');
		
		while($Entry = @readdir($MyDirectory)) {
			if($Entry != '.' && $Entry != '..') {
				if(is_dir($Directory.'/'.$Entry)) {
					$output .= '<option disabled class="pageavantaccueil_folder">'.str_repeat('&nbsp;', $Tab+2).$Entry.'</option>'."\n";
					//$output .= $this->ScanDirectory($Directory.'/'.$Entry, $Tab+10);
				}
				else {
					$output .= '<option '.(Configuration::get('pageavantaccueil_prior_home_page') == $Entry ? 'selected' : '').' value="'.$Entry.'" class="pageavantaccueil_file">'.str_repeat('&nbsp;', $Tab).$Entry.'</option>'."\n";
				}
			}
		}
		closedir($MyDirectory);
		return $output;
	}
	
	public function getContent()
	{
		$output = '<h2>'.$this->displayName.'</h2>';
		$erreur = false;
		
		if (Tools::isSubmit('submitConfig'))
		{
			Configuration::updateValue('pageavantaccueil_activate', 			Tools::getValue('pageavantaccueil_activate'));
			Configuration::updateValue('pageavantaccueil_time_cookie', 			Tools::getValue('pageavantaccueil_time_cookie'));
			Configuration::updateValue('pageavantaccueil_prior_home_page', 		Tools::getValue('pageavantaccueil_prior_home_page'));
			unset($_COOKIE['pageavantaccueil_page_load']);
		}
		return $output.$this->displayForm();
	}
	
	public function displayForm()
	{
		$output = '
			<form action="'.$_SERVER['REQUEST_URI'].'" method="post">
				<fieldset style="float:left;width:600px;">
					<legend>
						<img src="'.$this->_path.'logo.gif" alt="" title="" />'.$this->l('Settings').'
					</legend>
					<label>'.$this->l('General module activation').'</label>
					<div class="margin-form" style="padding-top:5px;">
						<input type="radio" name="pageavantaccueil_activate" id="pageavantaccueil_activate_on" value="1" '.(Tools::getValue('pageavantaccueil_activate', Configuration::get('pageavantaccueil_activate')) ? 'checked="checked" ' : '').'/>
						<label class="t" for="pageavantaccueil_activate_on"> <img src="../img/admin/enabled.gif" alt="'.$this->l('Enabled').'" title="'.$this->l('Enabled').'" /></label>
						<input type="radio" name="pageavantaccueil_activate" id="pageavantaccueil_activate_off" value="0" '.(!Tools::getValue('pageavantaccueil_activate', Configuration::get('pageavantaccueil_activate')) ? 'checked="checked" ' : '').'/>
						<label class="t" for="pageavantaccueil_activate_off"> <img src="../img/admin/disabled.gif" alt="'.$this->l('Disabled').'" title="'.$this->l('Disabled').'" /></label>
						<p class="clear">'.$this->l('Set true to activate the module').'</p>
					</div>
					<div class="full"></div>
					<label>'.$this->l('Time before the new prior home page appears').'</label>
					<div class="margin-form">
						<select name="pageavantaccueil_time_cookie">
							<option value="15" '.(Configuration::get('pageavantaccueil_time_cookie') == '15' ? 'selected' : '').'>15 '.$this->l('seconds').'</option>
							<option value="30" '.(Configuration::get('pageavantaccueil_time_cookie') == '30' ? 'selected' : '').'>30 '.$this->l('seconds').'</option>
							<option value="60" '.(Configuration::get('pageavantaccueil_time_cookie') == '60' ? 'selected' : '').'>1 '.$this->l('minute').'</option>
							<option value="120" '.(Configuration::get('pageavantaccueil_time_cookie') == '120' ? 'selected' : '').'>2 '.$this->l('minutes').'</option>
							<option value="300" '.(Configuration::get('pageavantaccueil_time_cookie') == '300' ? 'selected' : '').'>5 '.$this->l('minutes').'</option>
							<option value="600" '.(Configuration::get('pageavantaccueil_time_cookie') == '600' ? 'selected' : '').'>10 '.$this->l('minutes').'</option>
							<option value="900" '.(Configuration::get('pageavantaccueil_time_cookie') == '900' ? 'selected' : '').'>15 '.$this->l('minutes').'</option>
							<option value="1800" '.(Configuration::get('pageavantaccueil_time_cookie') == '1800' ? 'selected' : '').'>30 '.$this->l('minutes').'</option>
							<option value="3600" '.(Configuration::get('pageavantaccueil_time_cookie') == '3600' ? 'selected' : '').'>1 '.$this->l('hour').'</option>
							<option value="7200" '.(Configuration::get('pageavantaccueil_time_cookie') == '7200' ? 'selected' : '').'>2 '.$this->l('hours').'</option>
							<option value="18000" '.(Configuration::get('pageavantaccueil_time_cookie') == '18000' ? 'selected' : '').'>5 '.$this->l('hours').'</option>
							<option value="86400" '.(Configuration::get('pageavantaccueil_time_cookie') == '86400' ? 'selected' : '').'>1 '.$this->l('day').'</option>
							<option value="172800" '.(Configuration::get('pageavantaccueil_time_cookie') == '172800' ? 'selected' : '').'>2 '.$this->l('days').'</option>
							<option value="604800" '.(Configuration::get('pageavantaccueil_time_cookie') == '604800' ? 'selected' : '').'>1 '.$this->l('week').'</option>
							<option value="1209600" '.(Configuration::get('pageavantaccueil_time_cookie') == '1209600' ? 'selected' : '').'>2 '.$this->l('weeks').'</option>
							<option value="2678400" '.(Configuration::get('pageavantaccueil_time_cookie') == '2678400' ? 'selected' : '').'>1 '.$this->l('month').'</option>
							<option value="5356800" '.(Configuration::get('pageavantaccueil_time_cookie') == '5356800' ? 'selected' : '').'>2 '.$this->l('months').'</option>
							<option value="7948800" '.(Configuration::get('pageavantaccueil_time_cookie') == '7948800' ? 'selected' : '').'>3 '.$this->l('months').'</option>
							<option value="31536000" '.(Configuration::get('pageavantaccueil_time_cookie') == '31536000' ? 'selected' : '').'>1 '.$this->l('year').'</option>
						</select>
						<p class="clear">'.$this->l('During this period, the visitor will not see the prior web home page.').'</p>
					</div>
					<label>'.$this->l('Choose the prior home page').'</label>
					<div class="margin-form">
						<style>
							.pageavantaccueil_folder {background: url(../img/admin/folder.gif) no-repeat;height:20px;font-weight: bold;}
							.pageavantaccueil_file {background: url(../img/admin/translation.gif) no-repeat;height:15px;}
						</style>
						<select name="pageavantaccueil_prior_home_page">
							'.$this->ScanDirectory('../', 5).'
						</select>
						<p class="clear">'.$this->l('You can create your proper prior home page and put this page into the web site root.<br />Then choose your file in this select.').'</p>
					</div>
					<center><input type="submit" name="submitConfig" value="'.$this->l('Save').'" class="button" /></center>
				</fieldset>
				<iframe style="float:right;width:250px;height:300px;border: 1px solid #888;" src="http://m.dev-net.fr/frame.php"></iframe>
			<div class="clear"></div>
			</form>';
		return $output;
	}
	
}

?>
